package com.manager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.manager.pojo.TbUser;
import com.manager.service.UserService;
import com.manager.vo.TbUserEx;

/**
 * 
 * @Title:  UserController.java     
 * @Description:    账号信息获取controller
 * @author:      
 * @date:   2019年2月14日 下午3:36:55   
 * @version V1.0
 */
@Controller
public class UserControl {

	@Autowired
	private UserService userService;
	
	@RequestMapping("/user-adds")
	@ResponseBody			//添加账号
	public TbUserEx userAdds(TbUser tbUser) {
		
		TbUserEx retTbUserEx = new TbUserEx();
		//判断用户名是否为空
		if(tbUser.getUsername().isEmpty()) {
			
			retTbUserEx.setCode(400);
			retTbUserEx.setError("用户名不能为空");
			return retTbUserEx;
		}
		//判断用户名长度
		if(tbUser.getUsername().length()<6) {
			
			retTbUserEx.setCode(400);
			retTbUserEx.setError("用户名不能小于6位字符");
			return retTbUserEx;
		}
		//判断密码是否为空
		if(tbUser.getPassword().isEmpty()) {
			
			retTbUserEx.setCode(400);
			retTbUserEx.setError("密码不能为空");
			return retTbUserEx;
		}
		//判断密码位数
		if(tbUser.getPassword().length()<6) {
			
			retTbUserEx.setCode(400);
			retTbUserEx.setError("密码不能小于6位字符");
			return retTbUserEx;
		}
		
		try {		//添加用户
			retTbUserEx = userService.addUser(tbUser);
		}catch(Exception e) {
			
			e.printStackTrace();
			retTbUserEx.setCode(500);
			retTbUserEx.setError("账号添加异常");
			return retTbUserEx;
		}
		return retTbUserEx;
	}
	
	
	@RequestMapping("/user-del")
	@ResponseBody			//删除账号
	public TbUserEx userDel(Integer id) {
		
		TbUserEx retTbUserEx = null;
		
		try {			//删除用户
			retTbUserEx = userService.delUser(id);
		}catch(Exception e) {
			
			e.printStackTrace();
			retTbUserEx.setCode(500);
			retTbUserEx.setError("账号删除异常");
			return retTbUserEx;
		}
		
		retTbUserEx.setCode(200);
		retTbUserEx.setMsg("账号删除成功");
		
		return retTbUserEx;
	}
	
	@RequestMapping("/user-edit")
	@ResponseBody		//编辑账号信息
	public TbUserEx userEdit(TbUser tbUser) {
		
		TbUserEx retTbUserEx = new TbUserEx();
		//用户名是否为空
		if(tbUser.getUsername().isEmpty()) {
			
			retTbUserEx.setCode(400);
			retTbUserEx.setError("用户名不能为空");
			return retTbUserEx;
		}
		//判断用户名长度
		if(tbUser.getUsername().length()<6) {
			
			retTbUserEx.setCode(400);
			retTbUserEx.setError("用户名不能小于6位字符");
			return retTbUserEx;
		}
		//密码是否为空
		if(tbUser.getPassword().isEmpty()) {
			
			retTbUserEx.setCode(400);
			retTbUserEx.setError("密码不能为空");
			return retTbUserEx;
		}
		//判断密码长度
		if(tbUser.getPassword().length()<6) {
			
			retTbUserEx.setCode(400);
			retTbUserEx.setError("密码不能小于6位字符");
			return retTbUserEx;
		}
		
		retTbUserEx = userService.editUser(tbUser);
		
		return retTbUserEx;
	}
	
	
	@RequestMapping("/get-user")	//id获取账号信息
	public String getUser(Integer id,Model model) {
		
		TbUser tbUser = null;
		
		try {
			tbUser = userService.getUserById(id);
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		model.addAttribute("tbUser", tbUser);
		
		return "user-edit";
	}
	
	
	@RequestMapping("/user-list")		//获取账号分页列表
	public String userList(Integer page,Integer rows,Model model) {
		
		TbUserEx tbUserEx = null;
		
		try {
			tbUserEx = userService.getUserListInfo(page, rows);
		} catch (Exception e) {

			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("账号信息获取异常");
			return "user-list";
		}
		
		model.addAttribute("tbUserEx", tbUserEx);
		tbUserEx.setCode(200);
		tbUserEx.setMsg("账号信息获取成功");
		
		return "user-list";
	}
	
	
}
